import { lazy, Suspense, useEffect } from 'react';
import {
  Navigate,
  RouteObject,
  RouterProvider,
  createBrowserRouter,
  createHashRouter,
} from 'react-router-dom';

import DashboardLayout from '@/layouts/dashboard';
import KnowDashboardLayout from '@/layouts/know-layouts/dashboard';
import WorkDashboardLayout from '@/layouts/work-layouts/index';
import tiangongAiRoutes from '@/pages/tiangong-ai/routes';
import AuthGuard from '@/router/components/auth-guard';
import { usePermissionRoutes } from '@/router/hooks';
import { ErrorRoutes } from '@/router/routes/error-routes';

import { AppRouteObject } from '@/types/router';
import ShareAuthGuard from './components/share-link-auth';
import { useKnowPermissionRoutes, useWorkBasePermissionRoutes } from './hooks/use-permission-routes';
import { useConfigActions } from '@/store/configStore';


const { VITE_APP_HOMEPAGE: HOMEPAGE } = import.meta.env;
const LoginRoute: AppRouteObject = {
  path: '/login',
  Component: lazy(() => import('@/pages/sys/login/Login')),
};
const ShareRoute: AppRouteObject = {
  // path: '/apps/share/:id/:sid/:did/:mid',
  path: '/apps/share/:id/:sid',
  Component: lazy(() => import('@/pages/chat')),
};
const ShareOldRoute: AppRouteObject = {
  path: '/apps/share/:id/:sid/:did/:mid',
  Component: lazy(() => import('@/pages/chat')),
};
const AuthRoute: AppRouteObject = {
  path: '/auth',
  Component: lazy(() => import('@/pages/auth')),
};
const PAGE_NOT_FOUND_ROUTE: AppRouteObject = {
  path: '*',
  element: <Navigate to="/404" replace />,
};





export default function Router() {
  const permissionRoutes = usePermissionRoutes();
  const asyncRoutes: AppRouteObject = {
    path: '/',
    element: (
      <AuthGuard>
        <DashboardLayout />
      </AuthGuard>
    ),
    children: [{ index: true, element: <Navigate to={HOMEPAGE} replace /> }, ...permissionRoutes],
  };


  const workBasePermissionRoutes = useWorkBasePermissionRoutes();

  const workRoutes: AppRouteObject = {
    path: '/worklayouts',
    element: (
      <AuthGuard>
        <WorkDashboardLayout />
      </AuthGuard>
    ),
    children: [
      ...workBasePermissionRoutes,
    ],
  };

  const knowPermissionRoutes = useKnowPermissionRoutes();
  // const { fetchInitialInfo } = useConfigActions();
  // useEffect(() => {
  //   const init = async () => {
  //     fetchInitialInfo();
  //   };
  //   init();
  // }, []);

  const knowRoutes: AppRouteObject = {
    path: '/know',
    element: (
      <AuthGuard>
        <KnowDashboardLayout />
      </AuthGuard>
    ),
    children: [
      { index: true, element: <Navigate to={'datasets/list'} replace /> },
      ...knowPermissionRoutes,
    ],
  };

  const AuthCheckPage = lazy(() => import('@/pages/auth-check/index'));

  const ShareRoute: AppRouteObject = {
    path: '/sharelink/:share_code/:app_id/:share_id',
    Component: () => (
      <Suspense>
        <ShareAuthGuard>
          <AuthCheckPage />
        </ShareAuthGuard>
      </Suspense>
    ),
  };
  const routes = [
    LoginRoute,
    ...tiangongAiRoutes,
    asyncRoutes,
    knowRoutes,
    ErrorRoutes,
    ShareRoute,
    ShareOldRoute,
    AuthRoute,
    PAGE_NOT_FOUND_ROUTE,

    workRoutes
  ];
  const router = createBrowserRouter(routes as unknown as RouteObject[]);

  return <RouterProvider router={router} />;
}
